//
//  MainViewController.m
//  clublbiza
//
//  Created by WANG TOM on 7/26/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MainViewController.h"
#import "MenuitemViewController.h"
@interface MainViewController ()

@end

@implementation MainViewController
@synthesize tabMenuUIViewController;
@synthesize menuViewController;
@synthesize sliderUIViewController;
@synthesize timer;
@synthesize mainBackgroundImage;

@synthesize menuViewPanel,sliderimageViewPanel,tabbarViewPanel,tabbarGestureViewPanel;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void) dealloc
{
    [self.tabMenuUIViewController release];

    
    [self.menuViewController release];
    [self.sliderUIViewController release];
    [self.timer release];

    
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self refreshBackGrounImage];
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(currentSelectedMenuVersionIdChanged)
                                                 name: @"currentSelectedMenuVersionIdChanged"
                                               object: nil];
    
   
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(currentSelectedMenuTabIndexChanged)
                                                 name: @"currentSelectedMenuTabIndexChanged"
                                               object: nil];
    
  
    
    
    
    UITapGestureRecognizer *tapGusture = [[UITapGestureRecognizer alloc]
                                          initWithTarget:self
                                          action:@selector(handleViewTapGesture:)];
    [self.tabbarGestureViewPanel addGestureRecognizer:tapGusture];
    [tapGusture release];
    
}
- (void) handleTimerHideMenu
{
    UIView *menuView = self.tabbarViewPanel;    
    if ( menuView.alpha == 0.0 ) {
        return;
    }
    
     //[menuView setHidden:YES];
    CGPoint location =  menuView.center;
    location.y += 50;
    [UIView animateWithDuration:1 animations:^{
        menuView.alpha = 0.0;
        menuView.center = location;
    }];
    
    [self.tabbarGestureViewPanel setHidden:NO];
}
- (void) handleViewTapGesture:(UISwipeGestureRecognizer *)recognizer
{
   //CGPoint location = [recognizer locationInView:self.view];
   // NSLog(@"x:%f x:%f",location.x,location.y);
    
    
    UIView *menuView = self.tabbarViewPanel;  
    if ( menuView.alpha == 1 ) {
        return;
    }
    //[menuView setHidden:NO];
    CGPoint location =  menuView.center;
    location.y -= 50;
    [UIView animateWithDuration:1 animations:^{
        menuView.alpha = 1;
        menuView.center = location;
    }];
    
    [self.tabbarGestureViewPanel setHidden:YES];
    
    [self.timer invalidate];
    self.timer = [NSTimer scheduledTimerWithTimeInterval: 8  
                                                  target: self  
                                                selector: @selector(handleTimerHideMenu)  
                                                userInfo: nil  
                                                 repeats: NO];
}
- (void) currentSelectedMenuTabIndexChanged
{
    [self.timer invalidate];
   
    

    //bring views
    AppMain *appMain = [AppMain shareInstance];
    int menuTabIndex = appMain.currentSelectedMenuTabIndex;
    if ( menuTabIndex > -1 &&  menuTabIndex < 3 ) {
        [self.sliderimageViewPanel setHidden:YES];
        [self.menuViewPanel setHidden:NO];
        
       // [self.view bringSubviewToFront:self.menuViewPanel];
        //[self.view bringSubviewToFront:self.tabbarViewPanel];
        
        /*
        self.timer = [NSTimer scheduledTimerWithTimeInterval: 8  
                                                      target: self  
                                                    selector: @selector(handleTimerHideMenu)  
                                                    userInfo: nil  
                                                     repeats: NO];
         */
    } else {
        [self.sliderimageViewPanel setHidden:NO];
        //[self.menuViewPanel setHidden:YES];
        //[self handleTimerHideMenu];
    }
    
}
-(void)currentSelectedMenuVersionIdChanged
{
       
    MenuitemViewController *vc = [[MenuitemViewController alloc] init];
    
    [ self.navigationController pushViewController:vc animated:YES];
    [vc release];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [self.tabMenuUIViewController release];
    self.tabMenuUIViewController = nil;
    [self.menuViewController release];
    self.menuViewController = nil;
    self.mainBackgroundImage = nil;
    
     self.menuViewPanel = nil;
     self.sliderimageViewPanel = nil;
     self.tabbarViewPanel = nil;
     self.tabbarGestureViewPanel = nil;

    
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES];
    [super viewWillAppear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}

- (void) refreshBackGrounImage
{
    NSString *defaultKeyName = APPLICATION_BACKGROUND_IMAGE_NAME_2;
    
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *isCustomize = [userDefaults stringForKey:defaultKeyName];
    if (isCustomize != nil){
        NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        
        
        NSString *bgImgPath = [documentsPath stringByAppendingFormat:@"/%@",defaultKeyName];
        UIImage *image = [UIImage imageWithContentsOfFile:bgImgPath];
        self.mainBackgroundImage.image = image;
    }else {
        UIImage *image = [UIImage imageNamed:@"bg2.jpg"];
        self.mainBackgroundImage.image = image;
    }
}
@end
